|
- Tricks with classes
- Instantiating instances in a function call
- -a way to add class elements to a list.
It is possible to fill an array or list with class elements by
instantiating the elements as they are passed to a function that
does the adding. Here is example code. The class 'A' represents the
data to be added, while the class 'B' represents a list implemented
with an array. // Test of instantiating an instance as a function parameter
// File: instantiate.cpp
#include <iostream>
#include <conio.h> // for getche
const int MAX = 10; // Number of elements in the list
class A
{public:
A(int x = 0, double y = 0) : one(x), two(y) {}
void Print();
private:
int one;
double two;
};
class B
{public:
B();
void Add (A item) {things[numElements++] = item;}
void Print();
private:
A things[MAX];
int numElements;
};
B :: B()
{numElements= 0;
}
void B :: Print()
{for (int i = 0; i < numElements; i++)
{ things[i].Print();
}
}
void A :: Print()
{cout << "\nThe instance holds a " << one << " and a " << two;
}
void main()
{ B stuff;
for (int i = 0; i < MAX; i++)
{ stuff.Add(A(i, i * 1.1));
// Here is the key line. Note how 'Add' is called with
a parameter that really is a call to a constructor!
}
stuff.Print();
cout << "\n\nHit any key to exit.";
getche();
}
- Using the Standard Template Library (STD)
- Use the Win32 platform to compile programs using the STD.
- Removing unwanted warnings about "expanded inline".
Click on Options|Project|Messages|Inefficient C++
Coding. Uncheck the top box: "Functions containing 'ident' not
expanded inline" (If this does not work, Click on "Messages" by
itself and make sure the "Message Set" is set to "Selected". If it
is set to "All", all warnings that are on by default are
displayed.)
- To make the standard template library namespace available to the
whole program, entry the code:
- using namespace std;
Use this after including at least one header file involving an
element of the standard template library
Example code:
#include <iostream>
#include <string>
using namespace std;
void main()
{
string s1;
cout << "Please enter a string";
cin >> s1;
cout << "You entered: " << s1;
cout << "Hit a key to exit"; // Assumes one is using the Win32 platform.
cin.get();
}
- Using the vector class to implement a simple, searchable
list.
One often wants to create a list of class elements that is
searchable. Although the list or map classes are often better for
this, a simple way of doing this is with a vector. Here is some
example code. Class 'A' is a simple structure with two properties.
The first can be viewed as a 'key' field. The second field
represents the data. Of course, one could simply add more fields
to create a more complex 'record'.
// Test of vector class for simple database type work
// File: vectorAsDB.cpp
#include <iostream>
#include <vector>
#include <algorithm> // for the 'find' function
#include <conio.h> // for getche
using namespace std;
const int MAX = 10; // Number of elements in the vector
class A
{public:
A(int x = 0, double y = 0) : one(x), two(y) {}
friend bool operator== (const A& a, const A& b) // needed by the vector class
{return a.one == b.one;}
void Print();
private:
int one;
double two;
};
void A :: Print()
{cout << "\nThe instance holds a " << one << " and a " << two;
}
void main()
{ vector v1;
for (int i = 0; i < MAX; i++)
{ v1.push_back(A(i, i * 1.1));
}
A a(3); // We will look for an element in the vector with a first element of 3
vector :: iterator iter = find(v1.begin(), v1.end(), a);
iter -> Print();
cout << "\n\nHit any key to exit.";
getche();
}
- Input Output Operations
- Using the getche() function to exit a program:
When you want any character to immediately cause a program to
exit, use the function getche(). To use this, you need to include
the file conio.h. Don't forget to write a prompt telling the user
to enter a key to exit the program.
- Requesting the name of a file to open:
- We would like to use the built-in string classes to read in the
name of files to be opened. Unfortunately, the open member functions
and constructors for the stream classes expect to receive char* -
based strings. Therefore, if we use instances of a string class, we
need to convert those strings to char*'s. Here is an example of such
code:
#include <cstring.h>
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
void main()
{ string s1("data.txt");
const char* fileNm = s1.c_str();
ofstream file(fileNm);
getche();
}
- Operator Precedence
Associativity:
- Most operators associate from left to right meaning that if
two operators in the same instruction have the same precedence,
the one of the left executes first. Some operators (the unary
operators and '?') associate from right to left. There are few
times when this makes a difference but here is one that
demonstrates the idea even if it is a bit far fetched. Assume that
the + and - unary operators have been overloaded on strings where
'+' means increment each letter to the next letter but if the
letter is a 'z', don't change it AND '-' means decrement each
letter to the previous letter but if the letter is 'a', don't
change it. Thus:
string s1(abz)
cout << s1
outputs "bcz"
and string s1(abz)
cout << s1
outputs "aay".
Now, if write the code: string s1(abz);
cout << +s1+-s1; // the second '+' represents the binary operation
// of concatenating the two strings.
we might expect to get the result "bczaby" - assuming that
the first '+' executes first. However, we will actually get
"bbzaay" because the '-' operator executes first.
- Run Time Arguments
Real programs often expect one or more command line arguments,
that is, arguments provided to the program when it is first called.
For example, if one wanted to write a program to copy one file to
another, one might want to simply type in the command, such as "Copy",
followed by the names of the two files, as in:
- Copy file.in filecopy.out
One can run such commands from inside the Borland IDE using the
debugger. Since the debugger in version 5.0 of Borland C++ works best
when compiling for console mode, the project should be set for such.
To pass run-time arguments to a program you want to run
- Choose Options|Environment|Debugger from the main menu
- In the "Run Arguments" box, enter the arguments you want to pass
to your program when it starts.
The function 'main' must be set up to receive such arguments.
To accomplish this, declare 'main' as follows:
void main(int argc, char** argv)
The parameter 'argc' will hold the number of arguments
passed to the program, with the program name itself and its full path
counted as the first argument. The second parameter, 'argv' is a bit
more complex. A declaration such as char* argv declares a pointer to a
char which, as we know, can really be considered as a pointer to the
first character in an array of characters. One can consider this a
string. The second '*' in the declaration, therefore, can be seen as
declaring a pointer to a string which, using the same idea as with a
char*, can be considered a pointer to an array of strings. That is
what argv actually is: a pointer to an array of strings, with each
element in the array representing one the arguments sent to the
program. Consider the simple program:
void main(int argc, char** argv)
{ if (argc == 2) // The name of the program and some other argument was sent.
{ ifstream in(argv[1]);
// code to handle a possible failure to open the file
// code to do something with the file
cout << "The complete path of the program is: " << argv[0];
}
}
|